package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.TurnstileDataParam;
import com.project.hqshorttaxi.entity.TurnstileData;
import com.project.hqshorttaxi.mapper.TurnstileDataMapper;
import com.project.hqshorttaxi.service.TurnstileDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TurnstileDataServiceImpl implements TurnstileDataService {

    @Resource
    private TurnstileDataMapper turnstileDataMapper;

    @Override
    public TurnstileData getById(Integer id) {
        return turnstileDataMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(TurnstileData entity) throws Exception {
        turnstileDataMapper.insertSelective(entity);
    }

    @Override
    public TurnstileData getByShortDistanceId(Integer id) {
        Example example = new Example(TurnstileData.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("shortDistanceID", id);
        List<TurnstileData> list = turnstileDataMapper.selectByExample(example);
        return list.size() == 0 ? null : list.get(0);
    }

    @Override
    public List<TurnstileData> list(TurnstileDataParam param) {
        Example example = new Example(TurnstileData.class);
        Example.Criteria c = example.createCriteria();
        if (!StringUtils.isEmpty(param.getPlateNumber())) {
            c.andLike("plateNumber", "%" + param.getPlateNumber() + "%");
        }
        if (param.getParkingNo() != null) {
            c.andEqualTo("parkingNo", param.getParkingNo());
        }
        if (param.getBeginTime() != null) {
            c.andGreaterThanOrEqualTo("turnTime", param.getBeginTime());
        }
        if (param.getEndTime() != null) {
            c.andLessThanOrEqualTo("turnTime", param.getEndTime());
        }
        c.andIsNotNull("whiteListID");
        example.orderBy("turnTime").desc();
        List<TurnstileData> list = turnstileDataMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<TurnstileData> getByWhiteList(String plateNumber, String date) {
        Example example = new Example(TurnstileData.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("plateNumber", plateNumber);
        c.andEqualTo("opDate", date);
        c.andIsNotNull("whiteListID");
        List<TurnstileData> list = turnstileDataMapper.selectByExample(example);
        return list;
    }
}
